from django.db import models
# 引用内置的user模型
from django.contrib.auth.models import AbstractUser
# 导入基础类
from myutils.base_model import BaseModel
# Create your models here.


class User(BaseModel, AbstractUser):
    """
    继承了django的用户表，在这里重写用户表，在里面加字段
    """
    phone = models.CharField(max_length=11, verbose_name='手机号', blank=True, null=True)
    icon = models.ImageField(upload_to='icon', verbose_name='头像', blank=True, null=True)
    is_active = models.BooleanField(default=0)

    class Meta:
        db_table = 'user'

    def __str__(self):
        return self.username


class AuthUser(models.Model):
    """
    三方登录关联表
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    weibo = models.CharField(max_length=128, verbose_name='uid', null=True, blank=True)
    qq_openid = models.CharField(max_length=128, verbose_name='qqopenid', null=True, blank=True)

    class Meta:
        db_table = 'auth_user'

    def __str__(self):
        return self.user.username


class UserToken(BaseModel):
    """
    用户token表
    """
    user = models.OneToOneField(User, on_delete=models.CASCADE)  # 联级删除 当主键被删除时，同时删除外键
    token = models.CharField(max_length=256)

    class Meta:
        db_table = 'user_token'  # 为表起别名

    def __str__(self):
        return self.token






